package com.drinks.yiniang.ui.activity.community;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.goods.MyCommentsAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.common.recycler.decoration.MyCommentsDecoration;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.io.api.ShoppingCartApiIO;
import com.drinks.yiniang.ui.activity.goods.GoodsDetailsActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;

/**
 * 我的点评
 *
 * @author han
 * @date 2019/09/10
 */
public class MyCommentsActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {

    //nav
    private NavigationBarLayout nav_bar_my_comments;
    private BaseRecyclerView rcv_my_comments;

    private MyCommentsAdapter mMyCommentsAdapter;

    private int mPages;

    @Override
    protected void initView() {
        nav_bar_my_comments = findViewById(R.id.nav_bar_my_comments);

        rcv_my_comments = findViewById(R.id.rcv_my_comments);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
        getMyComments();
    }

    private void initAdapter() {
        rcv_my_comments.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 2));
        rcv_my_comments.addItemDecoration(MyCommentsDecoration.getItemDPDecoration(10));

        mMyCommentsAdapter = new MyCommentsAdapter(mContext, new ArrayList<GoodsJson.GoodsList>());
        mMyCommentsAdapter.setCenterEmpty(getLayoutInflater(), rcv_my_comments);
        mMyCommentsAdapter.bindToRecyclerView(rcv_my_comments);
    }

    @Override
    protected void initListener() {
        nav_bar_my_comments.setOnNavgationBarClickListener(this);

        mMyCommentsAdapter.setOnItemClickListener(this);
        mMyCommentsAdapter.setOnItemChildClickListener(this);
        mMyCommentsAdapter.setOnLoadMoreListener(this, rcv_my_comments);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_my_comments;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mMyCommentsAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent(mContext, GoodsDetailsActivity.class);
        intent.putExtra(Constants.KeyGoodsId, data.getId());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mMyCommentsAdapter.getItem(position);

        if (data == null) {
            return;
        }

        switch (view.getId()) {
            case R.id.iv_home_goods_add_cart://添加到购物车
                addToCart(data.getId());
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        GoodsApiIO.getInstance().getMyGoodsComments(mPages, new APIRequestCallback<GoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(GoodsJson res) {
                if(mMyCommentsAdapter == null) {
                    return;
                }

                mPages++;

                mMyCommentsAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mMyCommentsAdapter.loadMoreComplete();
                } else {
                    mMyCommentsAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mMyCommentsAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //获取我的点评
    private void getMyComments() {
        showLoadingDialog();

        mPages = 1;

        GoodsApiIO.getInstance().getMyGoodsComments(mPages, new APIRequestCallback<GoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(GoodsJson res) {
                if(mMyCommentsAdapter == null) {
                    return;
                }

                mPages++;

                mMyCommentsAdapter.getData().clear();
                mMyCommentsAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mMyCommentsAdapter.loadMoreComplete();
                } else {
                    mMyCommentsAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mMyCommentsAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void addToCart(String goodsId) {
        //设置传参object
        showLoadingDialog();

        ShoppingCartApiIO.getInstance().addToShopCart(goodsId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(getString(R.string.text_add_success));
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
